const Product = require('../models/Product');
const ProductVideo = require('../models/ProductVideo');
const tiktokScraperAdvanced = require('../services/tiktokScraperAdvanced');
const tiktokScraperSmart = require('../services/tiktokScraperSmart');
const shoplusService = require('../services/shoplusService');
const videoDataService = require('../services/videoDataService');
const completeFlowService = require('../services/completeFlowService');
const { Op } = require('sequelize');

/**
 * 从TikTok商品链接中提取商品ID
 */
function extractProductId(url) {
  try {
    const match = url.match(/\/product\/(\d+)/);
    return match ? match[1] : null;
  } catch (error) {
    console.error('提取商品ID失败:', error);
    return null;
  }
}

class ProductController {
  constructor() {
    // 绑定方法以确保this上下文正确
    this.processCompleteFlow = this.processCompleteFlow.bind(this);
    this.processCompleteFlowWithVideos = this.processCompleteFlowWithVideos.bind(this);
    this.processCompleteThreeStepsFlow = this.processCompleteThreeStepsFlow.bind(this);
    this.saveCompleteFlowResults = this.saveCompleteFlowResults.bind(this);
  }
  /**
   * 处理商品链接，提取信息并保存到数据库
   */
  async processProductLink(ctx) {
    try {
      const { url } = ctx.request.body;

      if (!url) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品链接'
        };
        return;
      }

      // 验证是否为TikTok商品链接
      if (!url.includes('shop.tiktok.com') || !url.includes('/product/')) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供有效的TikTok商品链接'
        };
        return;
      }

      // 验证链接是否包含必要的参数
      const urlObj = new URL(url);
      const requiredParams = ['region', 'local', 'source'];
      const missingParams = requiredParams.filter(param => !urlObj.searchParams.has(param));

      if (missingParams.length > 0) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: `链接缺少必要参数: ${missingParams.join(', ')}`,
          example: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&local=en&source=product_detail'
        };
        return;
      }

      // 提取商品ID
      const productId = extractProductId(url);
      if (!productId) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '无法从链接中提取商品ID'
        };
        return;
      }

      // 检查商品是否已存在
      const existingProduct = await Product.findByPk(productId);
      if (existingProduct) {
        ctx.body = {
          success: true,
          message: '商品已存在',
          data: existingProduct
        };
        return;
      }

      // 使用智能爬虫进行真实网络请求
      console.log('使用智能爬虫获取真实商品数据');
      let productInfo = await tiktokScraperSmart.scrapeProductInfo(url);

      // 如果智能爬虫失败，尝试使用高级爬虫
      if (!productInfo.success) {
        console.log('智能爬虫失败，尝试使用高级爬虫...');
        try {
          productInfo = await tiktokScraperAdvanced.scrapeProductInfo(url);
        } catch (advancedError) {
          console.log('高级爬虫也失败了:', advancedError.message);
          // 如果所有爬虫都失败，返回错误而不是使用模拟数据
          ctx.status = 500;
          ctx.body = {
            success: false,
            message: '无法获取商品信息，请稍后重试',
            error: productInfo.error || advancedError.message
          };
          return;
        }
      }

      // 如果仍然失败，返回错误
      if (!productInfo.success) {
        let message = '无法获取商品信息，请稍后重试';
        let status = 500;

        // 检查是否是页面错误（如地区限制等）
        if (productInfo.isPageError) {
          console.log(`检测到页面错误，记录失败日志: ${productInfo.error} - ${productInfo.message}`);

          // 根据错误类型设置不同的状态码和消息
          switch (productInfo.error) {
            case 'REGION_RESTRICTED':
              message = '商品在当前地区不可用';
              status = 451; // Unavailable For Legal Reasons
              break;
            case 'PRODUCT_NOT_FOUND':
              message = '商品不存在或页面未找到';
              status = 404;
              break;
            case 'PRODUCT_UNAVAILABLE':
              message = '商品已下架或不再可用';
              status = 410; // Gone
              break;
            case 'ACCESS_DENIED':
              message = '访问被拒绝';
              status = 403;
              break;
            case 'LOGIN_REQUIRED':
              message = '需要登录才能访问';
              status = 401;
              break;
            case 'NETWORK_ERROR':
              message = '网络连接失败，无法访问目标网站';
              status = 502; // Bad Gateway
              break;
            case 'DNS_ERROR':
              message = 'DNS解析失败，无法找到服务器';
              status = 502; // Bad Gateway
              break;
            default:
              message = productInfo.message || '页面显示错误信息';
              status = 400;
          }

          ctx.status = status;
          ctx.body = {
            success: false,
            message: message,
            error: productInfo.error,
            details: `页面上显示的错误原因: ${productInfo.message}`,
            isPageError: true
          };
          return;
        }

        // 根据错误类型提供更具体的信息
        if (productInfo.error === 'CAPTCHA_REQUIRED') {
          message = '遇到验证码，需要人工处理或稍后重试';
          status = 429; // Too Many Requests
        } else if (productInfo.error === 'TITLE_NOT_FOUND') {
          message = '页面加载成功但未找到商品信息';
          status = 404;
        }

        ctx.status = status;
        ctx.body = {
          success: false,
          message: message,
          error: productInfo.error,
          details: productInfo.message || '请检查链接是否正确或稍后重试'
        };
        return;
      }

      // 保存到数据库
      const product = await Product.create({
        id: productId,
        productName: productInfo.title,
        priceUsd: productInfo.priceUsd,
        price: productInfo.price,
        sellerName: productInfo.sellerName,
        weekSoldCount: productInfo.weekSoldCount,
        weekSales: productInfo.weekSales,
        weekSalesUsd: productInfo.weekSalesUsd,
        totalSales: productInfo.totalSales,
        totalSalesUsd: productInfo.totalSalesUsd,
        relatedVideos: productInfo.relatedVideos,
        relatedAuthor: productInfo.relatedAuthor
      });

      ctx.body = {
        success: true,
        message: '商品信息保存成功',
        data: product
      };

    } catch (error) {
      console.error('处理商品链接失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 获取商品信息
   */
  async getProduct(ctx) {
    try {
      const { id } = ctx.params;

      const product = await Product.findByPk(id);
      if (!product) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      ctx.body = {
        success: true,
        data: product
      };

    } catch (error) {
      console.error('获取商品信息失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 获取所有商品列表
   */
  async getProducts(ctx) {
    try {
      const {
        page = 1,
        limit = 10,
        sortBy = 'createTime',
        sortOrder = 'DESC',
        search = ''
      } = ctx.query;

      const offset = (page - 1) * limit;

      // 构建查询条件
      const whereClause = {};
      if (search) {
        whereClause[Op.or] = [
          { productName: { [Op.like]: `%${search}%` } },
          { sellerName: { [Op.like]: `%${search}%` } }
        ];
      }

      // 构建排序
      const order = [[sortBy, sortOrder.toUpperCase()]];

      const { count, rows } = await Product.findAndCountAll({
        where: whereClause,
        order: order,
        limit: parseInt(limit),
        offset: offset
      });

      ctx.body = {
        success: true,
        data: {
          products: rows,
          pagination: {
            page: parseInt(page),
            limit: parseInt(limit),
            total: count,
            pages: Math.ceil(count / limit)
          }
        }
      };

    } catch (error) {
      console.error('获取商品列表失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 更新商品信息
   */
  async updateProduct(ctx) {
    try {
      const { id } = ctx.params;
      const updateData = ctx.request.body;

      // 移除不允许更新的字段
      delete updateData.id;
      delete updateData.createTime;

      const [updatedRowsCount] = await Product.update(updateData, {
        where: { id: id }
      });

      if (updatedRowsCount === 0) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      // 获取更新后的商品信息
      const product = await Product.findByPk(id);

      ctx.body = {
        success: true,
        message: '商品信息更新成功',
        data: product
      };

    } catch (error) {
      console.error('更新商品信息失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 重新爬取商品信息并更新
   */
  async refreshProduct(ctx) {
    try {
      const { id } = ctx.params;

      // 检查商品是否存在
      const existingProduct = await Product.findByPk(id);
      if (!existingProduct) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      // 构建商品链接进行重新爬取
      const url = `https://shop.tiktok.com/view/product/${id}`;

      // 首先尝试使用基础爬虫
      let productInfo = await tiktokScraper.scrapeProductInfo(url);

      // 如果基础爬虫失败，使用高级爬虫
      if (!productInfo.success) {
        console.log('基础爬虫失败，使用高级爬虫重新获取...');
        productInfo = await tiktokScraperAdvanced.scrapeProductInfo(url);
      }

      if (!productInfo.success) {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: '重新获取商品信息失败',
          error: productInfo.error
        };
        return;
      }

      // 更新商品信息
      const updateData = {
        productName: productInfo.title,
        priceUsd: productInfo.priceUsd,
        price: productInfo.price,
        sellerName: productInfo.sellerName,
        weekSoldCount: productInfo.weekSoldCount,
        weekSales: productInfo.weekSales,
        weekSalesUsd: productInfo.weekSalesUsd,
        totalSales: productInfo.totalSales,
        totalSalesUsd: productInfo.totalSalesUsd,
        relatedVideos: productInfo.relatedVideos,
        relatedAuthor: productInfo.relatedAuthor
      };

      await Product.update(updateData, {
        where: { id: id }
      });

      // 获取更新后的商品信息
      const product = await Product.findByPk(id);

      ctx.body = {
        success: true,
        message: '商品信息刷新成功',
        data: product
      };

    } catch (error) {
      console.error('刷新商品信息失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 删除商品
   */
  async deleteProduct(ctx) {
    try {
      const { id } = ctx.params;

      const deletedRowsCount = await Product.destroy({
        where: { id: id }
      });

      if (deletedRowsCount === 0) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      ctx.body = {
        success: true,
        message: '商品删除成功'
      };

    } catch (error) {
      console.error('删除商品失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 测试高级爬虫（专门处理滑块验证）
   */
  async testAdvancedScraper(ctx) {
    try {
      const { url } = ctx.request.body;

      if (!url) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品链接'
        };
        return;
      }

      console.log('使用高级爬虫测试链接:', url);

      // 直接使用高级爬虫
      const productInfo = await tiktokScraperAdvanced.scrapeProductInfo(url);

      ctx.body = {
        success: productInfo.success,
        message: productInfo.success ? '高级爬虫测试成功' : '高级爬虫测试失败',
        data: productInfo,
        scraperType: 'advanced'
      };

    } catch (error) {
      console.error('高级爬虫测试失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '高级爬虫测试失败',
        error: error.message
      };
    }
  }

  /**
   * 关闭浏览器资源
   */
  async closeBrowser(ctx) {
    try {
      await tiktokScraperAdvanced.closeBrowser();
      ctx.body = {
        success: true,
        message: '浏览器资源已释放'
      };
    } catch (error) {
      console.error('关闭浏览器失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '关闭浏览器失败',
        error: error.message
      };
    }
  }



  /**
   * 测试智能爬虫
   */
  async testSmartScraper(ctx) {
    try {
      const { url } = ctx.request.body;

      if (!url) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品链接'
        };
        return;
      }

      console.log('测试智能爬虫:', url);

      const productInfo = await tiktokScraperSmart.scrapeProductInfo(url);
      const stats = tiktokScraperSmart.getStats();

      ctx.body = {
        success: productInfo.success,
        message: productInfo.success ? '智能爬虫测试成功' : '智能爬虫测试失败',
        data: productInfo,
        stats: stats,
        features: [
          '自动处理App推广弹窗',
          '智能滑块验证处理',
          '访问频次控制',
          '模拟真实用户行为'
        ]
      };

    } catch (error) {
      console.error('智能爬虫测试失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '智能爬虫测试失败',
        error: error.message
      };
    }
  }

  /**
   * 获取爬虫统计信息
   */
  async getScraperStats(ctx) {
    try {
      const smartStats = tiktokScraperSmart.getStats();

      ctx.body = {
        success: true,
        data: {
          smartScraper: smartStats,
          timestamp: new Date().toISOString(),
          features: {
            appModalHandling: '自动点击"Not Now"关闭App推广弹窗',
            captchaHandling: '智能滑块验证处理',
            frequencyControl: '访问频次控制，模拟真实用户',
            humanBehavior: '随机延迟和鼠标轨迹模拟'
          }
        }
      };

    } catch (error) {
      console.error('获取统计信息失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取统计信息失败',
        error: error.message
      };
    }
  }

  /**
   * 关闭所有爬虫资源
   */
  async closeAllScrapers(ctx) {
    try {
      await Promise.all([
        tiktokScraperAdvanced.closeBrowser(),
        tiktokScraperSmart.closeBrowser()
      ]);

      ctx.body = {
        success: true,
        message: '所有爬虫资源已释放'
      };
    } catch (error) {
      console.error('关闭爬虫资源失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '关闭爬虫资源失败',
        error: error.message
      };
    }
  }

  /**
   * 步骤二：根据商品名称查询Shoplus API并更新数据库
   */
  async searchAndUpdateProduct(ctx) {
    try {
      const { id, productName } = ctx.request.body;

      if (!id || !productName) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品ID和商品名称'
        };
        return;
      }

      // 检查商品是否存在
      const existingProduct = await Product.findByPk(id);
      if (!existingProduct) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      // 检查token是否过期
      if (shoplusService.isTokenExpired()) {
        ctx.status = 401;
        ctx.body = {
          success: false,
          message: 'Shoplus授权token已过期，请更新token'
        };
        return;
      }

      console.log(`开始搜索商品: ${productName}`);

      // 调用Shoplus API搜索商品
      const searchResult = await shoplusService.searchProducts(productName);

      if (!searchResult.success) {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: 'Shoplus API搜索失败',
          error: searchResult.error,
          details: searchResult.message
        };
        return;
      }

      // 解析商品数据
      let parsedData;
      try {
        parsedData = shoplusService.parseProductData(searchResult.data);
      } catch (parseError) {
        console.error('解析商品数据失败:', parseError);
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: '解析商品数据失败',
          error: parseError.message
        };
        return;
      }

      if (!parsedData) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '未找到匹配的商品数据'
        };
        return;
      }

      // 更新数据库中的商品信息
      const updateData = {
        rank: parsedData.rank,
        priceUsd: parsedData.priceUsd,
        price: parsedData.price,
        sellerName: parsedData.sellerName,
        weekSoldCount: parsedData.weekSoldCount,
        weekSales: parsedData.weekSales,
        weekSalesUsd: parsedData.weekSalesUsd,
        totalSalesUsd: parsedData.totalSalesUsd,
        totalSales: parsedData.totalSales,
        relatedVideos: parsedData.relatedVideos,
        relatedAuthor: parsedData.relatedAuthor
      };

      await Product.update(updateData, {
        where: { id: id }
      });

      // 获取更新后的商品信息
      const updatedProduct = await Product.findByPk(id);

      ctx.body = {
        success: true,
        message: '商品信息更新成功',
        data: {
          product: updatedProduct,
          shoplusData: parsedData,
          searchResults: searchResult.data.length
        }
      };

    } catch (error) {
      console.error('搜索并更新商品失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 根据商品ID自动搜索并更新（使用数据库中的商品名称）
   */
  async autoSearchAndUpdate(ctx) {
    try {
      const { id } = ctx.params;

      // 获取商品信息
      const product = await Product.findByPk(id);
      if (!product) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      if (!product.productName) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '商品名称为空，无法进行搜索'
        };
        return;
      }

      // 截取商品名称的前几个关键词，避免搜索字符串过长
      const keywords = product.productName.split(' ').slice(0, 8).join(' ');

      // 检查token是否过期
      if (shoplusService.isTokenExpired()) {
        ctx.status = 401;
        ctx.body = {
          success: false,
          message: 'Shoplus授权token已过期，请更新token'
        };
        return;
      }

      console.log(`开始自动搜索商品: ${keywords}`);

      // 调用Shoplus API搜索商品
      const searchResult = await shoplusService.searchProducts(keywords);

      if (!searchResult.success) {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: 'Shoplus API搜索失败',
          error: searchResult.error,
          details: searchResult.message
        };
        return;
      }

      // 解析商品数据
      let parsedData;
      try {
        parsedData = shoplusService.parseProductData(searchResult.data);
      } catch (parseError) {
        console.error('解析商品数据失败:', parseError);
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: '解析商品数据失败',
          error: parseError.message
        };
        return;
      }

      if (!parsedData) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '未找到匹配的商品数据'
        };
        return;
      }

      // 更新数据库中的商品信息
      const updateData = {
        rank: parsedData.rank,
        priceUsd: parsedData.priceUsd,
        price: parsedData.price,
        sellerName: parsedData.sellerName,
        weekSoldCount: parsedData.weekSoldCount,
        weekSales: parsedData.weekSales,
        weekSalesUsd: parsedData.weekSalesUsd,
        totalSalesUsd: parsedData.totalSalesUsd,
        totalSales: parsedData.totalSales,
        relatedVideos: parsedData.relatedVideos,
        relatedAuthor: parsedData.relatedAuthor
      };

      await Product.update(updateData, {
        where: { id: id }
      });

      // 获取更新后的商品信息
      const updatedProduct = await Product.findByPk(id);

      ctx.body = {
        success: true,
        message: '商品信息自动更新成功',
        data: {
          product: updatedProduct,
          shoplusData: parsedData,
          searchResults: searchResult.data.length,
          searchKeywords: keywords
        }
      };

    } catch (error) {
      console.error('自动搜索更新失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 测试Shoplus API搜索功能
   */
  async testShoplusSearch(ctx) {
    try {
      const { keyword } = ctx.request.body;

      if (!keyword) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供搜索关键词'
        };
        return;
      }

      // 检查token是否过期
      if (shoplusService.isTokenExpired()) {
        ctx.status = 401;
        ctx.body = {
          success: false,
          message: 'Shoplus授权token已过期，请更新token'
        };
        return;
      }

      console.log('测试Shoplus搜索:', keyword);

      const searchResult = await shoplusService.searchProducts(keyword);

      if (searchResult.success) {
        const parsedData = shoplusService.parseProductData(searchResult.data);

        ctx.body = {
          success: true,
          message: 'Shoplus搜索测试成功',
          data: {
            searchResults: searchResult.data,
            parsedData: parsedData,
            totalResults: searchResult.data.length
          }
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: 'Shoplus搜索测试失败',
          error: searchResult.error,
          details: searchResult.message
        };
      }

    } catch (error) {
      console.error('Shoplus搜索测试失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '测试失败',
        error: error.message
      };
    }
  }

  /**
   * 一次性完整流程：TikTok URL → 商品名称 → Shoplus API → 完整商品数据 → 数据库
   * 融合步骤一和步骤二的完整接口
   */
  async processCompleteFlow(ctx) {
    try {
      const { url } = ctx.request.body;

      if (!url) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供TikTok商品链接'
        };
        return;
      }

      // 验证是否为TikTok商品链接
      if (!url.includes('shop.tiktok.com') || !url.includes('/product/')) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供有效的TikTok商品链接'
        };
        return;
      }

      // 验证链接是否包含必要的参数
      const urlObj = new URL(url);
      const requiredParams = ['region', 'local', 'source'];
      const missingParams = requiredParams.filter(param => !urlObj.searchParams.has(param));

      if (missingParams.length > 0) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: `链接缺少必要参数: ${missingParams.join(', ')}`,
          example: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&local=en&source=product_detail'
        };
        return;
      }

      // 提取商品ID
      const productId = extractProductId(url);
      if (!productId) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '无法从链接中提取商品ID'
        };
        return;
      }

      console.log(`开始完整流程处理商品: ${productId}`);

      // 检查商品是否已存在
      let existingProduct = await Product.findByPk(productId);
      let isNewProduct = !existingProduct;

      // ========== 步骤一：TikTok URL → 商品名称 → 数据库 ==========
      let productInfo;
      if (isNewProduct) {
        console.log('步骤一：从TikTok获取基本商品信息...');

        // 使用智能爬虫进行真实网络请求
        productInfo = await tiktokScraperSmart.scrapeProductInfo(url);

        // 如果智能爬虫失败，尝试使用高级爬虫
        if (!productInfo.success) {
          console.log('智能爬虫失败，尝试使用高级爬虫...');
          try {
            productInfo = await tiktokScraperAdvanced.scrapeProductInfo(url);
          } catch (advancedError) {
            console.log('高级爬虫也失败了:', advancedError.message);
            ctx.status = 500;
            ctx.body = {
              success: false,
              message: '无法获取TikTok商品信息，请稍后重试',
              error: productInfo.error || advancedError.message,
              step: 'tiktok_scraping'
            };
            return;
          }
        }

        // 如果仍然失败，返回错误
        if (!productInfo.success) {
          let message = '无法获取TikTok商品信息，请稍后重试';
          let status = 500;

          // 检查是否是页面错误（如地区限制等）
          if (productInfo.isPageError) {
            console.log(`检测到页面错误，记录失败日志: ${productInfo.error} - ${productInfo.message}`);

            // 根据错误类型设置不同的状态码和消息
            switch (productInfo.error) {
              case 'REGION_RESTRICTED':
                message = '商品在当前地区不可用';
                status = 451; // Unavailable For Legal Reasons
                break;
              case 'PRODUCT_NOT_FOUND':
                message = '商品不存在或页面未找到';
                status = 404;
                break;
              case 'PRODUCT_UNAVAILABLE':
                message = '商品已下架或不再可用';
                status = 410; // Gone
                break;
              case 'ACCESS_DENIED':
                message = '访问被拒绝';
                status = 403;
                break;
              case 'LOGIN_REQUIRED':
                message = '需要登录才能访问';
                status = 401;
                break;
              case 'NETWORK_ERROR':
                message = '网络连接失败，无法访问目标网站';
                status = 502; // Bad Gateway
                break;
              case 'DNS_ERROR':
                message = 'DNS解析失败，无法找到服务器';
                status = 502; // Bad Gateway
                break;
              default:
                message = productInfo.message || '页面显示错误信息';
                status = 400;
            }

            ctx.status = status;
            ctx.body = {
              success: false,
              message: message,
              error: productInfo.error,
              details: `页面上显示的错误原因: ${productInfo.message}`,
              step: 'tiktok_page_error',
              skipSubsequentSteps: true // 标记跳过后续步骤
            };
            return;
          }

          if (productInfo.error === 'CAPTCHA_REQUIRED') {
            message = '遇到验证码，需要人工处理或稍后重试';
            status = 429;
          } else if (productInfo.error === 'TITLE_NOT_FOUND') {
            message = '页面加载成功但未找到商品信息';
            status = 404;
          }

          ctx.status = status;
          ctx.body = {
            success: false,
            message: message,
            error: productInfo.error,
            details: productInfo.message || '请检查链接是否正确或稍后重试',
            step: 'tiktok_scraping'
          };
          return;
        }

        // 保存基本商品信息到数据库
        existingProduct = await Product.create({
          id: productId,
          productName: productInfo.title,
          priceUsd: productInfo.priceUsd,
          price: productInfo.price,
          sellerName: productInfo.sellerName,
          weekSoldCount: productInfo.weekSoldCount,
          weekSales: productInfo.weekSales,
          weekSalesUsd: productInfo.weekSalesUsd,
          totalSales: productInfo.totalSales,
          totalSalesUsd: productInfo.totalSalesUsd,
          relatedVideos: productInfo.relatedVideos,
          relatedAuthor: productInfo.relatedAuthor
        });

        console.log('步骤一完成：基本商品信息已保存到数据库');
      } else {
        console.log('商品已存在，跳过步骤一，直接进行Shoplus搜索');
      }

      // ========== 步骤二：商品名称 → Shoplus API → 完整商品数据 → 数据库更新 ==========
      console.log('步骤二：使用Shoplus API获取详细销售数据...');

      if (!existingProduct.productName) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '商品名称为空，无法进行Shoplus搜索',
          step: 'shoplus_search'
        };
        return;
      }

      // 检查Shoplus token是否过期
      if (shoplusService.isTokenExpired()) {
        ctx.status = 401;
        ctx.body = {
          success: false,
          message: 'Shoplus授权token已过期，请更新token',
          step: 'shoplus_auth'
        };
        return;
      }

      // 截取商品名称的前几个关键词，避免搜索字符串过长
      const keywords = existingProduct.productName.split(' ').slice(0, 8).join(' ');
      console.log(`使用关键词搜索Shoplus: ${keywords}`);

      // 调用Shoplus API搜索商品
      const searchResult = await shoplusService.searchProducts(keywords);

      if (!searchResult.success) {
        // Shoplus搜索失败，但TikTok数据已保存，返回部分成功
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但Shoplus搜索失败，可稍后重试Shoplus更新' :
            '商品已存在，Shoplus搜索失败',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusError: searchResult.error,
            shoplusMessage: searchResult.message
          },
          warning: 'Shoplus API搜索失败',
          step: 'shoplus_search_failed'
        };
        return;
      }

      // 解析Shoplus商品数据
      let parsedData;
      try {
        parsedData = shoplusService.parseProductData(searchResult.data);
      } catch (parseError) {
        console.error('解析Shoplus数据失败:', parseError);
        // 解析失败，但TikTok数据已保存
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但Shoplus数据解析失败' :
            '商品已存在，Shoplus数据解析失败',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusRawData: searchResult.data
          },
          warning: 'Shoplus数据解析失败',
          step: 'shoplus_parse_failed'
        };
        return;
      }

      if (!parsedData) {
        // 未找到匹配数据，但TikTok数据已保存
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但未找到匹配的Shoplus数据' :
            '商品已存在，未找到匹配的Shoplus数据',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            searchResults: searchResult.data.length
          },
          warning: '未找到匹配的Shoplus商品数据',
          step: 'shoplus_no_match'
        };
        return;
      }

      // 更新数据库中的商品信息（合并Shoplus数据）
      const updateData = {
        rank: parsedData.rank,
        // 优先使用Shoplus的价格数据，如果没有则保持原有数据
        priceUsd: parsedData.priceUsd || existingProduct.priceUsd,
        price: parsedData.price || existingProduct.price,
        sellerName: parsedData.sellerName || existingProduct.sellerName,
        weekSoldCount: parsedData.weekSoldCount || existingProduct.weekSoldCount,
        weekSales: parsedData.weekSales || existingProduct.weekSales,
        weekSalesUsd: parsedData.weekSalesUsd || existingProduct.weekSalesUsd,
        totalSalesUsd: parsedData.totalSalesUsd || existingProduct.totalSalesUsd,
        totalSales: parsedData.totalSales || existingProduct.totalSales,
        relatedVideos: parsedData.relatedVideos || existingProduct.relatedVideos,
        relatedAuthor: parsedData.relatedAuthor || existingProduct.relatedAuthor
      };

      await Product.update(updateData, {
        where: { id: productId }
      });

      // 获取最终更新后的商品信息
      const finalProduct = await Product.findByPk(productId);

      console.log('步骤二完成：Shoplus数据已更新到数据库');

      // 返回完整流程成功结果
      ctx.body = {
        success: true,
        message: isNewProduct ?
          '完整流程成功：TikTok商品信息已获取并通过Shoplus API更新了详细销售数据' :
          '商品已存在，已通过Shoplus API更新了最新销售数据',
        data: {
          product: finalProduct,
          tiktokData: isNewProduct ? productInfo : null,
          shoplusData: parsedData,
          searchResults: searchResult.data.length,
          searchKeywords: keywords,
          isNewProduct: isNewProduct
        },
        steps: {
          step1_tiktok: isNewProduct ? 'completed' : 'skipped',
          step2_shoplus: 'completed'
        }
      };

    } catch (error) {
      console.error('完整流程处理失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '完整流程处理失败',
        error: error.message,
        step: 'unknown_error'
      };
    }
  }

  /**
   * 步骤三：获取单个商品的视频数据
   */
  async getProductVideos(ctx) {
    try {
      const { id } = ctx.params;

      if (!id) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品ID'
        };
        return;
      }

      // 检查商品是否存在
      const product = await Product.findByPk(id);
      if (!product) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      console.log(`开始获取商品视频数据: ${product.productName}`);

      // 使用视频数据服务获取视频信息
      const result = await videoDataService.processProductVideos(id, product.productName);

      if (result.success) {
        ctx.body = {
          success: true,
          message: '视频数据获取成功',
          data: {
            productId: id,
            productName: product.productName,
            videoCount: result.videoCount,
            totalVideos: result.totalVideos,
            failedVideos: result.failedVideos
          }
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: '视频数据获取失败',
          error: result.error,
          productId: id,
          productName: product.productName
        };
      }

    } catch (error) {
      console.error('获取商品视频失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 步骤三：批量获取多个商品的视频数据
   */
  async batchGetProductVideos(ctx) {
    try {
      const { limit = 10, offset = 0, onlyWithoutVideos = false } = ctx.request.body;

      // 构建查询条件
      let whereCondition = {};

      if (onlyWithoutVideos) {
        // 只处理还没有视频数据的商品
        const productsWithVideos = await ProductVideo.findAll({
          attributes: ['product_id'],
          group: ['product_id']
        });
        const productIdsWithVideos = productsWithVideos.map(pv => pv.product_id);

        whereCondition = {
          id: {
            [Op.notIn]: productIdsWithVideos
          }
        };
      }

      // 获取需要处理的商品列表
      const products = await Product.findAll({
        where: whereCondition,
        limit: parseInt(limit),
        offset: parseInt(offset),
        order: [['createTime', 'DESC']]
      });

      if (products.length === 0) {
        ctx.body = {
          success: true,
          message: '没有需要处理的商品',
          data: {
            processedCount: 0,
            totalVideos: 0
          }
        };
        return;
      }

      console.log(`开始批量处理 ${products.length} 个商品的视频数据`);

      // 批量处理视频数据
      const batchResult = await videoDataService.batchProcessProductVideos(products);

      ctx.body = {
        success: true,
        message: `批量处理完成，成功: ${batchResult.success}，失败: ${batchResult.failed}`,
        data: {
          total: batchResult.total,
          success: batchResult.success,
          failed: batchResult.failed,
          totalVideos: batchResult.totalVideos,
          details: batchResult.details
        }
      };

    } catch (error) {
      console.error('批量获取商品视频失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '批量处理失败',
        error: error.message
      };
    }
  }

  /**
   * 获取商品及其关联的视频数据
   */
  async getProductWithVideos(ctx) {
    try {
      const { id } = ctx.params;
      const { limit = 10, offset = 0 } = ctx.query;

      if (!id) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供商品ID'
        };
        return;
      }

      // 获取商品信息及其关联的视频
      const product = await Product.findByPk(id, {
        include: [{
          model: ProductVideo,
          as: 'videos',
          limit: parseInt(limit),
          offset: parseInt(offset),
          order: [['play_count', 'DESC']] // 按播放量降序排序
        }]
      });

      if (!product) {
        ctx.status = 404;
        ctx.body = {
          success: false,
          message: '商品不存在'
        };
        return;
      }

      // 获取视频总数
      const totalVideos = await ProductVideo.count({
        where: { product_id: id }
      });

      ctx.body = {
        success: true,
        message: '获取成功',
        data: {
          product: product,
          totalVideos: totalVideos,
          currentVideos: product.videos.length
        }
      };

    } catch (error) {
      console.error('获取商品视频数据失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '服务器内部错误',
        error: error.message
      };
    }
  }

  /**
   * 完整流程增强版：包含视频数据获取
   * 步骤一：TikTok商品信息 -> 步骤二：Shoplus搜索更新 -> 步骤三：视频数据获取
   */
  async processCompleteFlowWithVideos(ctx) {
    try {
      const { url, authToken } = ctx.request.body;

      if (!url) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供TikTok商品链接'
        };
        return;
      }

      console.log('🚀 开始完整流程（包含视频数据）:', url);
      if (authToken) {
        console.log('🔑 使用自定义authToken');
      } else {
        console.log('🔑 使用默认authToken');
      }

      // 验证是否为TikTok商品链接
      if (!url.includes('shop.tiktok.com') || !url.includes('/product/')) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供有效的TikTok商品链接'
        };
        return;
      }

      // 验证链接是否包含必要的参数
      const urlObj = new URL(url);
      const requiredParams = ['region', 'local', 'source'];
      const missingParams = requiredParams.filter(param => !urlObj.searchParams.has(param));

      if (missingParams.length > 0) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: `链接缺少必要参数: ${missingParams.join(', ')}`,
          example: 'https://shop.tiktok.com/view/product/1731808168774042656?region=MY&local=en&source=product_detail'
        };
        return;
      }

      // 提取商品ID
      const productId = extractProductId(url);
      if (!productId) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '无法从链接中提取商品ID'
        };
        return;
      }

      console.log(`开始完整流程处理商品: ${productId}`);

      // 检查商品是否已存在
      let existingProduct = await Product.findByPk(productId);
      let isNewProduct = !existingProduct;

      // ========== 步骤一：TikTok URL → 商品名称 → 数据库 ==========
      let productInfo;
      if (isNewProduct) {
        console.log('步骤一：从TikTok获取基本商品信息...');

        // 使用智能爬虫进行真实网络请求
        productInfo = await tiktokScraperSmart.scrapeProductInfo(url);

        // 如果智能爬虫失败，尝试使用高级爬虫
        if (!productInfo.success) {
          console.log('智能爬虫失败，尝试使用高级爬虫...');
          try {
            productInfo = await tiktokScraperAdvanced.scrapeProductInfo(url);
          } catch (advancedError) {
            console.log('高级爬虫也失败了:', advancedError.message);
            ctx.status = 500;
            ctx.body = {
              success: false,
              message: '无法获取TikTok商品信息，请稍后重试',
              error: productInfo.error || advancedError.message,
              step: 'tiktok_scraping'
            };
            return;
          }
        }

        // 如果仍然失败，返回错误
        if (!productInfo.success) {
          let message = '无法获取TikTok商品信息，请稍后重试';
          let status = 500;

          // 检查是否是页面错误（如地区限制等）
          if (productInfo.isPageError) {
            console.log(`检测到页面错误，记录失败日志: ${productInfo.error} - ${productInfo.message}`);

            // 根据错误类型设置不同的状态码和消息
            switch (productInfo.error) {
              case 'REGION_RESTRICTED':
                message = '商品在当前地区不可用';
                status = 451; // Unavailable For Legal Reasons
                break;
              case 'PRODUCT_NOT_FOUND':
                message = '商品不存在或页面未找到';
                status = 404;
                break;
              case 'PRODUCT_UNAVAILABLE':
                message = '商品已下架或不再可用';
                status = 410; // Gone
                break;
              case 'ACCESS_DENIED':
                message = '访问被拒绝';
                status = 403;
                break;
              case 'LOGIN_REQUIRED':
                message = '需要登录才能访问';
                status = 401;
                break;
              case 'NETWORK_ERROR':
                message = '网络连接失败，无法访问目标网站';
                status = 502; // Bad Gateway
                break;
              case 'DNS_ERROR':
                message = 'DNS解析失败，无法找到服务器';
                status = 502; // Bad Gateway
                break;
              default:
                message = productInfo.message || '页面显示错误信息';
                status = 400;
            }

            ctx.status = status;
            ctx.body = {
              success: false,
              message: message,
              error: productInfo.error,
              details: `页面上显示的错误原因: ${productInfo.message}`,
              step: 'tiktok_page_error',
              skipSubsequentSteps: true // 标记跳过后续步骤
            };
            return;
          }

          if (productInfo.error === 'CAPTCHA_REQUIRED') {
            message = '遇到验证码，需要人工处理或稍后重试';
            status = 429;
          } else if (productInfo.error === 'TITLE_NOT_FOUND') {
            message = '页面加载成功但未找到商品信息';
            status = 404;
          }

          ctx.status = status;
          ctx.body = {
            success: false,
            message: message,
            error: productInfo.error,
            details: productInfo.message || '请检查链接是否正确或稍后重试',
            step: 'tiktok_scraping'
          };
          return;
        }

        // 保存基本商品信息到数据库
        existingProduct = await Product.create({
          id: productId,
          productName: productInfo.title,
          priceUsd: productInfo.priceUsd,
          price: productInfo.price,
          sellerName: productInfo.sellerName,
          weekSoldCount: productInfo.weekSoldCount,
          weekSales: productInfo.weekSales,
          weekSalesUsd: productInfo.weekSalesUsd,
          totalSales: productInfo.totalSales,
          totalSalesUsd: productInfo.totalSalesUsd,
          relatedVideos: productInfo.relatedVideos,
          relatedAuthor: productInfo.relatedAuthor
        });

        console.log('步骤一完成：基本商品信息已保存到数据库');
      } else {
        console.log('商品已存在，跳过步骤一，直接进行Shoplus搜索');
      }

      // ========== 步骤二：商品名称 → Shoplus API → 完整商品数据 → 数据库更新 ==========
      console.log('步骤二：使用Shoplus API获取详细销售数据...');

      if (!existingProduct.productName) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '商品名称为空，无法进行Shoplus搜索',
          step: 'shoplus_search'
        };
        return;
      }

      // 检查Shoplus token是否过期
      if (shoplusService.isTokenExpired()) {
        ctx.status = 401;
        ctx.body = {
          success: false,
          message: 'Shoplus授权token已过期，请更新token',
          step: 'shoplus_auth'
        };
        return;
      }

      // 截取商品名称的前几个关键词，避免搜索字符串过长
      const keywords = existingProduct.productName.split(' ').slice(0, 8).join(' ');
      console.log(`使用关键词搜索Shoplus: ${keywords}`);

      // 调用Shoplus API搜索商品
      const searchResult = await shoplusService.searchProducts(keywords);

      if (!searchResult.success) {
        // Shoplus搜索失败，但TikTok数据已保存，返回部分成功
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但Shoplus搜索失败，可稍后重试Shoplus更新' :
            '商品已存在，Shoplus搜索失败',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusError: searchResult.error,
            shoplusMessage: searchResult.message
          },
          warning: 'Shoplus API搜索失败',
          step: 'shoplus_search_failed'
        };
        return;
      }

      // 解析Shoplus商品数据
      let parsedData;
      try {
        parsedData = shoplusService.parseProductData(searchResult.data);
      } catch (parseError) {
        console.error('解析Shoplus数据失败:', parseError);
        // 解析失败，但TikTok数据已保存
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但Shoplus数据解析失败' :
            '商品已存在，Shoplus数据解析失败',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusRawData: searchResult.data
          },
          warning: 'Shoplus数据解析失败',
          step: 'shoplus_parse_failed'
        };
        return;
      }

      if (!parsedData) {
        // 未找到匹配数据，但TikTok数据已保存
        ctx.body = {
          success: true,
          message: isNewProduct ?
            'TikTok商品信息已保存，但未找到匹配的Shoplus数据' :
            '商品已存在，未找到匹配的Shoplus数据',
          data: {
            product: existingProduct,
            tiktokData: isNewProduct ? productInfo : null,
            searchResults: searchResult.data.length
          },
          warning: '未找到匹配的Shoplus商品数据',
          step: 'shoplus_no_match'
        };
        return;
      }

      // 更新数据库中的商品信息（合并Shoplus数据）
      const updateData = {
        rank: parsedData.rank,
        // 优先使用Shoplus的价格数据，如果没有则保持原有数据
        priceUsd: parsedData.priceUsd || existingProduct.priceUsd,
        price: parsedData.price || existingProduct.price,
        sellerName: parsedData.sellerName || existingProduct.sellerName,
        weekSoldCount: parsedData.weekSoldCount || existingProduct.weekSoldCount,
        weekSales: parsedData.weekSales || existingProduct.weekSales,
        weekSalesUsd: parsedData.weekSalesUsd || existingProduct.weekSalesUsd,
        totalSalesUsd: parsedData.totalSalesUsd || existingProduct.totalSalesUsd,
        totalSales: parsedData.totalSales || existingProduct.totalSales,
        relatedVideos: parsedData.relatedVideos || existingProduct.relatedVideos,
        relatedAuthor: parsedData.relatedAuthor || existingProduct.relatedAuthor
      };

      await Product.update(updateData, {
        where: { id: productId }
      });

      // 获取最终更新后的商品信息
      const finalProduct = await Product.findByPk(productId);

      console.log('步骤二完成：Shoplus数据已更新到数据库');

      // ========== 步骤三：获取视频数据 ==========
      console.log('🎬 执行步骤三：获取视频数据...');
      const videoResult = await videoDataService.processProductVideos(productId, finalProduct.productName, authToken);

      // 合并结果
      if (videoResult.success) {
        ctx.body = {
          success: true,
          message: '完整流程（包含视频数据）执行成功',
          data: {
            product: finalProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusData: parsedData,
            searchResults: searchResult.data.length,
            searchKeywords: keywords,
            isNewProduct: isNewProduct,
            videoData: {
              videoCount: videoResult.videoCount,
              totalVideos: videoResult.totalVideos,
              failedVideos: videoResult.failedVideos,
              errors: videoResult.errors
            }
          },
          steps: {
            step1_tiktok: isNewProduct ? 'completed' : 'skipped',
            step2_shoplus: 'completed',
            step3_videos: 'completed'
          }
        };
        console.log('🎉 完整流程（包含视频数据）执行成功');
      } else {
        // 视频获取失败，但前两步成功
        ctx.body = {
          success: true, // 仍然返回成功，因为基本数据已获取
          message: '商品数据获取成功，但视频数据获取失败',
          data: {
            product: finalProduct,
            tiktokData: isNewProduct ? productInfo : null,
            shoplusData: parsedData,
            searchResults: searchResult.data.length,
            searchKeywords: keywords,
            isNewProduct: isNewProduct,
            videoData: {
              error: videoResult.error,
              videoCount: 0
            }
          },
          steps: {
            step1_tiktok: isNewProduct ? 'completed' : 'skipped',
            step2_shoplus: 'completed',
            step3_videos: 'failed'
          },
          warning: '视频数据获取失败'
        };
        console.log('⚠️ 商品数据成功，视频数据失败');
      }

    } catch (error) {
      console.error('完整流程（包含视频数据）处理失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '完整流程处理失败',
        error: error.message,
        step: 'complete_flow_with_videos_error'
      };
    }
  }

  /**
   * 完整的三步骤连贯操作
   * 步骤一：输入视频链接
   * 步骤二：根据商品名称查询商品列表，并将信息存储到t_product
   * 步骤三：循环点击商品列表，将商品详情下的视频解析，存储到t_product_videos
   */
  async processCompleteThreeStepsFlow(ctx) {
    try {
      const { videoLink, authToken } = ctx.request.body;

      if (!videoLink) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供视频链接'
        };
        return;
      }

      // 验证是否为TikTok视频链接
      if (!videoLink.includes('tiktok.com') || !videoLink.includes('/video/')) {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: '请提供有效的TikTok视频链接',
          example: 'https://www.tiktok.com/@username/video/7123456789012345678'
        };
        return;
      }

      console.log('🚀 开始完整的三步骤连贯操作...');
      console.log('输入视频链接:', videoLink);
      if (authToken) {
        console.log('使用自定义authToken');
      } else {
        console.log('使用默认authToken');
      }

      // 调用完整流程服务
      const result = await completeFlowService.processCompleteThreeSteps(videoLink, authToken);

      if (result.success) {
        // 将结果保存到数据库
        const saveResult = await this.saveCompleteFlowResults.call(this, result.data);

        ctx.body = {
          success: true,
          message: '完整的三步骤连贯操作成功',
          data: {
            ...result.data,
            databaseSave: saveResult
          },
          steps: {
            step1_video_to_product: result.step1?.success ? 'completed' : 'failed',
            step2_search_products: result.step2?.success ? 'completed' : 'failed',
            step3_get_videos: result.step3?.success ? 'completed' : 'failed'
          }
        };
      } else {
        ctx.status = 500;
        ctx.body = {
          success: false,
          message: result.message,
          error: result.error,
          steps: {
            step1_video_to_product: result.step1?.success ? 'completed' : 'failed',
            step2_search_products: result.step2?.success ? 'completed' : 'failed',
            step3_get_videos: result.step3?.success ? 'completed' : 'failed'
          },
          details: {
            step1: result.step1,
            step2: result.step2,
            step3: result.step3
          }
        };
      }

    } catch (error) {
      console.error('三步骤连贯操作失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '三步骤连贯操作处理失败',
        error: error.message
      };
    }
  }

  /**
   * 保存完整流程的结果到数据库
   */
  async saveCompleteFlowResults(data) {
    try {
      const saveResults = {
        products: { success: 0, failed: 0, errors: [] },
        videos: { success: 0, failed: 0, errors: [] }
      };

      // 保存商品数据
      if (data.searchResults && data.searchResults.products) {
        for (const product of data.searchResults.products) {
          try {
            // 检查商品是否已存在
            const existingProduct = await Product.findByPk(product.productId);

            if (!existingProduct) {
              // 创建新商品
              await Product.create({
                id: product.productId,
                productName: product.productName,
                rank: product.rank,
                priceUsd: product.priceUsd,
                price: product.price,
                sellerName: product.sellerName,
                weekSoldCount: product.weekSoldCount,
                weekSales: product.weekSales,
                weekSalesUsd: product.weekSalesUsd,
                totalSalesUsd: product.totalSalesUsd,
                totalSales: product.totalSales,
                relatedVideos: product.relatedVideos,
                relatedAuthor: product.relatedAuthor
              });
              saveResults.products.success++;
              console.log(`✅ 保存商品成功: ${product.productId}`);
            } else {
              // 更新现有商品
              await Product.update({
                rank: product.rank,
                priceUsd: product.priceUsd,
                price: product.price,
                sellerName: product.sellerName,
                weekSoldCount: product.weekSoldCount,
                weekSales: product.weekSales,
                weekSalesUsd: product.weekSalesUsd,
                totalSalesUsd: product.totalSalesUsd,
                totalSales: product.totalSales,
                relatedVideos: product.relatedVideos,
                relatedAuthor: product.relatedAuthor
              }, {
                where: { id: product.productId }
              });
              saveResults.products.success++;
              console.log(`🔄 更新商品成功: ${product.productId}`);
            }
          } catch (error) {
            saveResults.products.failed++;
            saveResults.products.errors.push({
              productId: product.productId,
              error: error.message
            });
            console.log(`❌ 保存商品失败: ${product.productId} - ${error.message}`);
          }
        }
      }

      // 保存视频数据
      if (data.videoData && data.videoData.productVideos) {
        for (const productVideo of data.videoData.productVideos) {
          for (const video of productVideo.videos) {
            try {
              // 检查视频是否已存在
              const existingVideo = await ProductVideo.findByPk(video.id);

              if (!existingVideo) {
                // 创建新视频记录
                await ProductVideo.create({
                  id: video.id,
                  origin_cover: video.originCover,
                  product_id: video.productId,
                  desc: video.desc,
                  video_duration: video.videoDuration,
                  video_link: video.videoLink,
                  total_estimated_sales_amount: video.totalEstimatedSalesAmount,
                  total_estimated_sales_volume: video.totalEstimatedSalesVolume,
                  play_count: video.playCount,
                  digg_count: video.diggCount,
                  comment_count: video.commentCount,
                  share_count: video.shareCount,
                  author_last_time: video.authorLastTime,
                  author_last_time_stamp: video.authorLastTimeStamp,
                  author_id: video.authorId,
                  author_unique_id: video.authorUniqueId,
                  author_nickname: video.authorNickname,
                  sys_country_country: video.sysCountryCountry,
                  sys_country_country_cn: video.sysCountryCountryCn,
                  sys_country_country_code: video.sysCountryCountryCode,
                  author_follower_count: video.authorFollowerCount,
                  author_homepage: video.authorHomepage
                });
                saveResults.videos.success++;
                console.log(`✅ 保存视频成功: ${video.id}`);
              } else {
                // 更新现有视频
                await ProductVideo.update({
                  origin_cover: video.originCover,
                  desc: video.desc,
                  video_duration: video.videoDuration,
                  video_link: video.videoLink,
                  total_estimated_sales_amount: video.totalEstimatedSalesAmount,
                  total_estimated_sales_volume: video.totalEstimatedSalesVolume,
                  play_count: video.playCount,
                  digg_count: video.diggCount,
                  comment_count: video.commentCount,
                  share_count: video.shareCount,
                  author_last_time: video.authorLastTime,
                  author_last_time_stamp: video.authorLastTimeStamp,
                  author_id: video.authorId,
                  author_unique_id: video.authorUniqueId,
                  author_nickname: video.authorNickname,
                  sys_country_country: video.sysCountryCountry,
                  sys_country_country_cn: video.sysCountryCountryCn,
                  sys_country_country_code: video.sysCountryCountryCode,
                  author_follower_count: video.authorFollowerCount,
                  author_homepage: video.authorHomepage
                }, {
                  where: { id: video.id }
                });
                saveResults.videos.success++;
                console.log(`🔄 更新视频成功: ${video.id}`);
              }
            } catch (error) {
              saveResults.videos.failed++;
              saveResults.videos.errors.push({
                videoId: video.id,
                productId: video.productId,
                error: error.message
              });
              console.log(`❌ 保存视频失败: ${video.id} - ${error.message}`);
            }
          }
        }
      }

      console.log('💾 数据库保存完成:');
      console.log(`商品: 成功 ${saveResults.products.success}, 失败 ${saveResults.products.failed}`);
      console.log(`视频: 成功 ${saveResults.videos.success}, 失败 ${saveResults.videos.failed}`);

      return saveResults;

    } catch (error) {
      console.error('保存数据库失败:', error);
      return {
        products: { success: 0, failed: 0, errors: [{ error: error.message }] },
        videos: { success: 0, failed: 0, errors: [{ error: error.message }] }
      };
    }
  }

  /**
   * 关闭所有服务
   */
  async closeAllServices(ctx) {
    try {
      // 关闭视频数据服务
      await videoDataService.closeBrowser();

      // 关闭现有的爬虫服务
      await tiktokScraperSmart.closeBrowser();
      await tiktokScraperAdvanced.closeBrowser();

      ctx.body = {
        success: true,
        message: '所有服务已关闭'
      };

    } catch (error) {
      console.error('关闭服务失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '关闭服务失败',
        error: error.message
      };
    }
  }
}

module.exports = new ProductController();